<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/8/20
 * Time: 15:45
 */

namespace App\Services;


use App\Models\RechargeRecord;
use App\Models\User;
use Illuminate\Support\Facades\DB;

class RechargeService extends BaseService
{

    public function getList($companyId, $data, int $size)
    {
        $order_sn = !empty($data['order_sn']) ? $data['order_sn'] : '';
        $status = !empty($data['status']) ? $data['status'] : '';
            $name = !empty($data['name']) ? $data['name'] : '';
        $phone = !empty($data['phone']) ? $data['phone'] : '';
        $price = !empty($data['price']) ? $data['price'] : '';
        $date = !empty($data['date']) ? $data['date'] : '';
        $list = RechargeRecord::query()
            ->select('id', 'order_sn', 'status', 'price', 'type', 'amount', 'after_price', 'created_at', 'user_id', 'company_id')
            ->with(['user:id,name,phone', 'company:id,name'])
            ->when($order_sn, function ($query) use ($order_sn) {
                return $query->where('order_sn', 'like', '%' . $order_sn . '%');   //订单号
            })
            ->when($status, function ($query) use ($status) {
                return $query->where('status', '=', $status);   //订单状态
            })
            ->when($name, function ($query) use ($name) {
                return $query->whereHas('company', function ($query) use ($name) {
                    return $query->where('name', 'like', '%' . $name . '%');   //公司名称
                });
            })
            ->when($phone, function ($query) use ($phone) {
                return $query->whereHas('user', function ($query) use ($phone) {
                    return $query->where('phone', 'like', '%' . $phone . '%');   //公司名称
                });
            })
            ->when($price, function ($query) use ($price) {
                return $query->where('price', 'like', '%' . $price . '%');   //充值金额
            })
            ->when($date, function ($query) use ($date) {
                if ($date) {
                    $list = explode(',', $date);
                    return $query->whereBetween('created_at', [$list[0], $list[1]]); //时间选择
                }
            })
            ->company($companyId)
            ->orderByDesc('created_at')
            ->paginate($size);
        return $list;
    }

    /**
     * @param int $user_id
     * @param $price
     * @throws \App\Exceptions\BobException
     * 用户充值
     */
    public function invest(int $user_id, $price)
    {
        try {
            DB::transaction(function () use ($user_id, $price) {
                User::query()->where('id', $user_id)->update([
                    'balance' => DB::raw('balance +' . $price)
                ]);
                $user = User::query()->find($user_id);
                $this->log($user, $price);
            });
        } catch (\Exception $exception) {
            return_bob($exception->getMessage());
        }

    }

    /**
     * @param object $user
     * @param $price
     * @return bool
     * 充值流水
     */
    public function log(object $user, $price)
    {
        $sn = uniqid('B');
        $recharge_record = new RechargeRecord();
        $recharge_record->user_id = $user->id;
        $recharge_record->price = $price;
        $recharge_record->status = 1;
        $recharge_record->order_sn = $sn;
        $recharge_record->note = '余额充值';
        $recharge_record->type = 1; //微信充值
        $recharge_record->after_price = bcadd($user->balance, $price, 2); //充值后金额
        $recharge_record->amount = $user->balance; //充值前金额
        if ($recharge_record->save()) {
            return true;
        };
        return false;
    }
}